<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.model' );
jimport( 'joomla.utilities.date' );
jimport( 'joomla.filesystem.file' );
jimport( 'joomla.filesystem.folder' );
jimport( 'joomla.access.rule' );

class jzmluvyModelUpravaFaktury extends Jmodel

{
  var $_data = null;
  var $_id = null;
  
	function __construct(){
    
    $this->params	= &JComponentHelper::getParams('com_jzmluvy');
    $id = JRequest::getVar( 'id', '', '', 'array' );
		$this->setId($id[0]);
    $this->nastavenie = & jzmluvyHelper::nastavenie();
    
		parent::__construct();
	}
	function setId($id)
	{
		// Set event id and wipe data
		$this->_id	    = $id;
		$this->_data	= null;
	}

  function getData()
	{
		if ($this->_loadData())
		{
		}
		else  $this->_initData();
		return $this->_data;
	}
  
  function _loadData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$query = 'SELECT *'
					. ' FROM #__jzmluvy'
					. ' WHERE id = '.$this->_id
					;
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();
			return (boolean) $this->_data;
		}
		return true;
	}
	
		function _initData()
	{
	
    // Lets load the content if it doesn't already exist
		//*** automaticke predvyplnanie ak sa pridava novy zaznam
    if (empty($this->_data) && $this->_id == '')
		{
			$now = JFactory::getDate();
			$end = JFactory::getDate();
			$end->_date += (60*60*24*$this->params->get( 'doba_vyveseni', ''));  
			
			$item = new stdClass();
			$item->id					= 0;
			$item->catid					= 0;
			$item->title				= '';
			$item->alias				= '';
			$item->created_by_alias		= '';
			$item->fulltext				= '';
			$item->jedcislo				= '';
			$item->publish_up				= '';
      $item->sposob_uhrady		= '';
      $item->vlastni1		= '';
      $item->typ		= '';
			$item->dodavatel				= '';
      $item->faktura		= '';
      $item->sposob		= '';
      $item->intCislo		= '';
      $item->suma		= '0';
      $item->datUhrady				= '';
      $item->datZver				=  $now->toFormat('%Y-%m-%d');
      $item->publish_down				= '';
			$item->mena				= $this->nastavenie->mena; //$this->params->get( 'mena', '');
			$this->_data				= $item;
			return (boolean) $this->_data;
		}
		//**** zachovanie vyplnenych poli pri zlom zapise
    if (empty($this->_data) && $this->_id == 0)
		{
			$post = JRequest::get( 'get' );
      
			$item = new stdClass();
		 	$item->id					= 0;
			$item->dodavatel				= $post['dodavatel'];
      $item->title				= $post['title'];
			$item->faktura				=  $post['faktura'];
			$item->intCislo				= $post['intCislo'];
      $item->suma				= $post['suma'];
      $item->mena				= $post['mena'];
      $item->sposob_uhrady				= $post['sposob_uhrady'];
      $item->vlastni1		= $post['vlastni1'];
      $item->sposob				= $post['sposob'];
      $item->typ				= $post['typ'];
      $item->publish_up				= $post['publish_up'];
			$item->publish_down				= $post['publish_down'];
			$item->datUhrady				= $post['datUhrady'];
      $item->datZver				= $post['datZver'];
      $item->file				= $post['file'];
      $item->file2				= $post['file2'];
      
      $this->_data				= $item;
			return (boolean) $this->_data;
		}
		return true;
	}
	function store($data)
	{
		global $mainframe;
		$user		= & JFactory::getUser();
		$row =& JTable::getInstance('upravaFaktury', '');
		// Bind the form fields to the table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

/*		
		// Check/sanitize the metatags
		$row->meta_description = htmlspecialchars(trim(addslashes($row->meta_description)));
		if (JString::strlen($row->meta_description) > 255) {
			$row->meta_description = JString::substr($row->meta_description, 0, 254);
		}
		$row->meta_keywords = htmlspecialchars(trim(addslashes($row->meta_keywords)));
		if (JString::strlen($row->meta_keywords) > 200) {
			$row->meta_keywords = JString::substr($row->meta_keywords, 0, 199);
		}
		//Check if image was selected
		jimport('joomla.filesystem.file');
		$format 	= JFile::getExt('JPATH_SITE/images/eventlist/events/'.$row->datimage);
		$allowable 	= array ('gif', 'jpg', 'png');
		if (in_array($format, $allowable)) {
			$row->datimage = $row->datimage;
		} else {
			$row->datimage = '';
		} */

    
    //*** pripona za nazov scen. suboru ak je zadefinovana
    if($this->params->get( 'pripona_scan' ) != '') {
    $pripona = '_'.JFilterOutput::stringURLSafe($this->params->get( 'pripona_scan' )); }
    else {
    $pripona = '';  
    }
    
    //*** pripona za nazov text. suboru ak je zadefinovana
    if($this->params->get( 'pripona_text' ) != '') {
    $pripona2 = '_'.JFilterOutput::stringURLSafe($this->params->get( 'pripona_text' )); }
    else {
    $pripona2 = '';  
    }
    
    //*** kontrola velikosti skenovaneho dokumenu
    if (!empty($data['file']['name'])) {
      $velikostSouboru = $this->params->get( 'max_velikost', '')*1048576;
      if ($data['file']['size'] > $velikostSouboru && $this->params->get( 'max_velikost', '') != 0) {
        $this->_error = JText::_( 'fa_EDIT_CHYBA_SUBOR_MAX_SCAN' ).'&nbsp;'.$this->params->get( 'max_velikost', '').'&nbsp;MB.&nbsp;';
        $this->_error .= JText::_( 'fa_EDIT_CHYBA_SUBOR_AKTUAL' ).'&nbsp;'. round($data['file']['size']/1048576,2).'&nbsp;MB.';
        JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
        return false;
			}
    }
    
    //*** kontrola velikosti textoveho dokumenu
    if (!empty($data['file2']['name'])) {
      $velikostSouboru = $this->params->get( 'max_velikost', '')*1048576;
      if ($data['file2']['size'] > $velikostSouboru && $this->params->get( 'max_velikost', '') != 0) {
        $this->_error = JText::_( 'fa_EDIT_CHYBA_SUBOR_MAX_TEXT' ).'&nbsp;'.$this->params->get( 'max_velikost', '').'&nbsp;MB.&nbsp; ';
        $this->_error .= JText::_( 'fa_EDIT_CHYBA_SUBOR_AKTUAL' ).'&nbsp;'. round($data['file2']['size']/1048576,2).'&nbsp;MB.';
        JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
        return false;
			}
    }
    
		// sanitise id field
		$row->id = (int) $row->id;
		$nullDate	= $this->_db->getNullDate();
    //uzivatel pridavajici nebo editujici zaznam
		if ($row->id) {
			$row->modified 		= gmdate('Y-m-d H:i:s');
			$row->modified_by 	= $user->get('id');
		} else {
			$row->modified 		= $nullDate;
			$row->modified_by 	= '';
			//get time and userid
			$row->created 			= gmdate('Y-m-d H:i:s');
//			$row->author_ip 		= $elsettings->storeip ? getenv('REMOTE_ADDR') : 'DISABLED';
			$row->created_by		= $user->get('id');
		}
	
  	//*** ulozenie nazvu skenovaneho dokumentu do DB na cely nazov; nazov suboru; priponu
    if (!empty($data['file']['name'])) {
      $row->file = $data['file']['name'];
      $row->filename = JFilterOutput::stringURLSafe(JFile::stripExt($data['file']['name'])).$pripona;
      $row->extension = strtolower(JFile::getExt($data['file']['name'])); 
    }
   
  	//*** ulozenie nazvu textovehou dokumentu do DB na cely nazov; nazov suboru; priponu
    if (!empty($data['file2']['name'])) {
      $row->file2 = $data['file2']['name'];
      $row->filename2 = JFilterOutput::stringURLSafe(JFile::stripExt($data['file2']['name'])).$pripona2;
      $row->extension2 = strtolower(JFile::getExt($data['file2']['name'])); 
    }

    //*** 
    if (!empty($data['soubor'])) {
      $row->file = $data['soubor']; 
    }
    if (!empty($data['soubor2'])) {
      $row->file2 = $data['soubor2']; 
    }
            
		// Make sure the data is valid
		if (!$row->check()) {
			$this->setError($row->getError());
			return false;
		}
		// Store the table to the database
		if (!$row->store(true)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		
		//*** ulozenie a struktura nazvu scanovaneho dokumentu
    if (!empty($data['file']['name'])) {
      $soubor = $data['file'];
      $nazevSouboru = JFile::stripExt($soubor);
      $priponaSouboru = JFile::getExt($soubor['name']);
      $nazevSouboru = $row->id.'-'.JFilterOutput::stringURLSafe($nazevSouboru['name']).$pripona;
      $nazevSouboru .= '.'.JFilterOutput::stringURLSafe($priponaSouboru);
      $src = $soubor['tmp_name'];
      $dest = JPATH_ROOT . DS . $this->params->get( 'adresar') . $nazevSouboru;
      JFile::upload($src, $dest);
    }

		//*** ulozenie a struktura nazvu textoveho dokumentu
    if (!empty($data['file2']['name'])) {
      $soubor = $data['file2'];
      $nazevSouboru = JFile::stripExt($soubor);
      $priponaSouboru = JFile::getExt($soubor['name']);
      $nazevSouboru = $row->id.'-'.JFilterOutput::stringURLSafe($nazevSouboru['name']).$pripona2;
      $nazevSouboru .= '.'.JFilterOutput::stringURLSafe($priponaSouboru);
      $src = $soubor['tmp_name'];
      $dest = JPATH_ROOT . DS . $this->params->get( 'adresar') . $nazevSouboru;
      JFile::upload($src, $dest);
    }

		return $row->id;
	}
}
?>